package kr.saeildamil.androidstudy_second.ani;

import java.util.ArrayList;

import kr.saeildamil.andrordstudy.second.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.ViewSwitcher.ViewFactory;

public class TextSwitcherTest extends Activity {
	TextSwitcher mTextSwitcher;
	ImageSwitcher mImageSwitcher;
	int mTextIdx;
	int mImageIdx;
	String[] texts;
	ArrayList<Drawable> imgs;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch23_textswitcher);
		
		texts = getResources().getStringArray(R.array.PLANET);
		
		imgs = new ArrayList<Drawable>();
		imgs.add(getResources().getDrawable(R.drawable.babydrum));
		imgs.add(getResources().getDrawable(R.drawable.babydrum1));
		imgs.add(getResources().getDrawable(R.drawable.korandoc));
		imgs.add(getResources().getDrawable(R.drawable.mugung));
		
		mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
		mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		
		mTextSwitcher.setFactory(mTextFactory);
		mTextSwitcher.setText(texts[mTextIdx]);
		
		mImageSwitcher.setFactory(mImageFactory);
		mImageSwitcher.setImageDrawable(imgs.get(mImageIdx));
	}
	ViewSwitcher.ViewFactory mTextFactory =  new ViewFactory() {
		public View makeView() {
			TextView text= new TextView(TextSwitcherTest.this);
			text.setTextSize(22);
			text.setBackgroundColor(Color.YELLOW);
			text.setTextColor(Color.BLACK);
			return text;
		}
	};
	
	ViewSwitcher.ViewFactory mImageFactory = new ViewFactory() {
		public View makeView() {
			ImageView img = new ImageView(TextSwitcherTest.this);
			return img;
		}
	};
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.textswitcher_textnext:
			mTextIdx = mTextIdx==texts.length-1?0:mTextIdx+1;
			mTextSwitcher.setText(texts[mTextIdx]);
			break;
		case R.id.textswitcher_imagenext:
			mImageIdx = mImageIdx==imgs.size()-1?0:mImageIdx+1;
			mImageSwitcher.setImageDrawable(imgs.get(mImageIdx));
			break;
		}
	}
}
